home *** CD-ROM | disk | FTP | other *** search
- /*==================================================================
- File: MacOS_UMemory.h
-
- Contains: Implementation of various Memory Manager utilities
-
- Written by: Ed Reed, Linden Siahaan
-
- Copyright: 2000 Connectix Corporation
- ==================================================================*/
-
- #include "MacOS_Exceptions.h"
- #include "MacOS_Namespaces.h"
-
- #ifndef __MACMEMORY__
- #include <MacMemory.h>
- #endif
-
- #include <new>
-
- #pragma once
-
- CTX_Begin_Namespace_MacOS
-
- /*------------------------------------------------------------------
- StHandleLocker
-
- This class implements a stack-based object which locks a handle.
- The destructor automatically unlocks the handle when the object
- goes out of scope.
-
- To use:
- Simply create a variable of type StHandleLocker within the
- scope you want access the locked handle. The destructor
- will automatically be called at the end of the scope.
- ------------------------------------------------------------------*/
-
- class StHandleLocker
- {
- public:
- StHandleLocker(Handle inMacHandle) : mMacHandle(inMacHandle)
- {
- mState = ::HGetState(inMacHandle);
- ::HLock(inMacHandle);
- check (MemError() == noErr);
- }
-
- ~StHandleLocker()
- {
- ::HSetState(mMacHandle, mState);
- }
-
- private:
- Handle mMacHandle;
- SInt8 mState;
- };
-
-
- /*------------------------------------------------------------------
- StHandleDisposer
-
- This class implements a stack-based object which automatically
- disposes of a handle when the object goes out of scope.
-
- To use:
- Simply create a variable of type StHandleDisposer within the
- scope you want access the handle. The destructor will
- automatically be called at the end of the scope.
- ------------------------------------------------------------------*/
-
- class StHandleDisposer
- {
- public:
-
- StHandleDisposer()
- {
- Set(NULL);
- }
-
- StHandleDisposer(
- Handle inData)
- {
- Set(inData);
- }
-
- ~StHandleDisposer()
- {
- if (mData != NULL)
- ::DisposeHandle(mData);
- }
-
- void
- Set(
- Handle inData)
- {
- mData = inData;
- }
-
- operator
- Handle() const
- {
- return mData;
- }
-
- protected:
- Handle mData;
- };
-
-
- /*------------------------------------------------------------------
- StPtrDisposer
-
- This class implements a stack-based object which automatically
- disposes of a Macintosh Ptr when the object goes out of scope.
-
- To use:
- Simply create a variable of type StPtrDisposer within the
- scope you want access the Ptr. The destructor will automatically
- be called at the end of the scope.
- ------------------------------------------------------------------*/
-
- class StPtrDisposer
- {
- public:
- StPtrDisposer()
- {
- Set(NULL);
- }
-
- StPtrDisposer(
- Ptr inData)
- {
- Set(inData);
- }
-
- ~StPtrDisposer()
- {
- if (mData != NULL)
- ::DisposePtr(mData);
- }
-
- void
- Set(
- Ptr inData)
- {
- mData = inData;
- }
-
- operator
- Ptr() const
- {
- return mData;
- }
-
- protected:
- Ptr mData;
- };
-
-
- /*------------------------------------------------------------------
- StArray<Type_>
-
- This template class implements a stack-based object which wraps
- a Ptr to the given type or an array of the given type. The
- destructor automatically disposes of the pointer when the
- object goes out of scope.
- ------------------------------------------------------------------*/
-
- template <class Type_> class StArray
- {
- public:
- StArray(
- ItemCount inArraySize = 1,
- Boolean inThrowFail = true,
- Boolean inClearBytes = false )
- {
- if (inClearBytes)
- {
- mPtr = reinterpret_cast<Type_*>( ::NewPtrClear( sizeof(Type_) * inArraySize ) );
- }
- else
- {
- mPtr = reinterpret_cast<Type_*>( ::NewPtr( sizeof(Type_) * inArraySize ) );
- }
-
- if (inThrowFail)
- {
- if (mPtr == NULL)
- throw std::bad_alloc();
- }
- }
-
- ~StArray()
- {
- if (mPtr != NULL)
- {
- ::DisposePtr( reinterpret_cast<Ptr>(mPtr) );
- }
- }
-
- // type coersions
- operator Type_*()
- {
- return mPtr;
- }
-
- Type_&
- operator[](
- ItemCount i)
- {
- return mPtr[i];
- }
-
- operator void*()
- {
- return reinterpret_cast<void*>(mPtr);
- }
-
- Type_
- Get() const
- {
- return mPtr;
- }
-
- protected:
- Type_* mPtr;
- };
-
-
- CTX_End_Namespace_MacOS
-
-
- /*==================================================================
- Change History (most recent first):
-
- $Log: MacOS_UMemory.h,v $
- Revision 1.13 2000/07/07 05:12:08 traut
- Carbon-related code cleanup.
-
- Revision 1.12 2000/05/30 23:09:52 cstory
- Moved a lot of stuff to the new MacOS_Accessors.h header
-
- Revision 1.11 2000/05/25 18:05:08 cstory
- Moved in class CZonerSwitcher from VirtualPCMiscUtils.h and re-wrote it
-
- Revision 1.10 2000/05/23 16:47:48 cstory
- Added dummy inlines for memory locking and unlocking under Carbon
-
- Revision 1.9 2000/05/17 16:29:39 cstory
- Added #pragma once
-
- Revision 1.8 2000/05/16 18:53:02 cstory
- Added setters and casting operators to StHandleDisposer and StPtrDisposer
-
- Revision 1.7 2000/05/15 22:56:54 cstory
- Added dummy inlines for HoldMemory() and UnholdMemory()
-
- Revision 1.6 2000/05/12 15:48:11 cstory
- Added StHandleDisposer and StPtrDisposer classes
-
- Revision 1.5 2000/03/16 19:19:53 reed
- if StArray fails, throw std::bad_alloc instead of PowerPlant exception
-
- Revision 1.4 2000/03/09 19:07:37 lsiahaan
- Correctly implemented MacOS namespaces, by including the header file that defines the macros.
-
- Revision 1.3 2000/03/09 08:53:09 traut
- Undid previous checkin. It broke VPC, VGS and BossHogg.
-
- Revision 1.2 2000/03/09 02:10:21 lsiahaan
- Implemented MacOS namespace, added StArray class
-
- Revision 1.1 1998/12/03 21:41:22 reed
- First Checked In.
- ==================================================================*/
-